<?php
namespace Home\Controller;
use Common\Controller\CommonController;

/**
 * 短信
 * @author 张亚伟 <zhangyw@zhijin101.com>
 */
class SmsController extends CommonController
{

    public function send()
    {
        if (IS_POST) {
            set_time_limit(0);
            $data = I('post.');
            if (empty($data['signname'])) {
                $this->ajaxReturn(array('s' => false, 'm' => '请输入签名'));
            }
            if (empty($data['model_id'])) {
                $this->ajaxReturn(array('s' => false, 'm' => '请输入模板id'));
            }
            if (!empty($data['mobiles'])){
                $mobiles = explode(',', $data['mobiles']);
            }else{
                $mobiles = D('user')->gropu('phone')->field('phone')->select();
            }
            foreach ($mobiles as $key => $mobile) {
                try {
                    Vendor('Alidayu.top.TopClient');
                    $c = new \TopClient;

                    $c->appkey = C('AlidayuAppKey');
                    $c->secretKey = C('AlidayuAppSecret');
                    Vendor('Alidayu.top.request.AlibabaAliqinFcSmsNumSendRequest');
                    $req = new \AlibabaAliqinFcSmsNumSendRequest;

                    // 短信类型，传入值请填写normal
                    $req->setSmsType("normal");

                    //【阿里大鱼】欢迎使用阿里大鱼服务。
                    $req->setSmsFreeSignName($data['signname']);
                    // $req->setSmsParam("{\"code\":\"$num\",\"name\":\"娱助理\"}");
                    //短信接收号码
                    $req->setRecNum($mobile);
                    //短信模板ID，传入的模板必须是在阿里大鱼“管理中心-短信模板管理”中的可用模板。示例：SMS_585014
                    $req->setSmsTemplateCode($data['model_id']);
                    $resp = $c->execute($req);
                } catch (\Exception $e) {
                    
                }
            }
            $this->ajaxReturn(array('s' => true, 'm' => '发送成功'));
        } else {
            $this->display();
        }
    }
}